/*
 * @Descripttion: 
 * @version: 
 * @Author: hzx
 * @Date: 2021-03-24 19:20:57
 * @LastEditors: hzx
 * @LastEditTime: 2021-03-27 22:14:32
 */

'use strict';

const Controller = require('egg').Controller;

class CourseController extends Controller {
    async index() {
        this.ctx.body = "api hi"
    }

    /**
     * @name: 获取所有学生得成绩信息
     * @msg: '学生成绩查询成功'
     * @param { object } ctx
     * @return {*}
     */
    async getCourseList() {
        let { t_id, sc_id } = this.ctx.query;
        //console.log(t_id,sc_id)
        if (t_id) {
            var sql = `SELECT * FROM stu_grade_view where t_id = ${t_id}`;
        }
        if (sc_id) {
            var sql = `SELECT * FROM stu_grade_view where sc_id = ${sc_id}`;
        }
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '学生成绩查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 获取班级信息
     * @msg: 
     * @param {*}
     * @return {*}
     */
    async getClassName() {
        let { t_id } = this.ctx.query;
        let sql = `Select distinct(class_name) FROM stu_grade_view where t_id = ${t_id}`
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 获取课程信息
     * @msg: 
     * @param {*}
     * @return {*}
     */
    async getCourseName() {
        let { t_id } = this.ctx.query;
        let sql = `Select distinct(course_name) FROM stu_grade_view where t_id = ${t_id}`
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        }
    }


    /**
     * @name: 根据课程名称，班级名称查询学生成绩
     * @msg: 
     * @param {*} course_name, class_name 非必填
     * @return {*} 
     */
    async selectCouCla() {
        let { courseName, className } = this.ctx.query;
        if (courseName === '请选择课程') {
            courseName = ''
        }
        if (className === '请选择班级') {
            className = ''
        }

        var sql = "SELECT * FROM stu_grade_view where 1=1 ";
        if (courseName) {
            var sql = sql + `and course_name='${courseName}'`
        }
        if (className) {
            var sql = sql + `and class_name='${className}'`
        }
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 更新学生成绩
     * @msg: 
     * @param {*} {grade, grade_id}必填
     * @return {*}
     */UPDATE 
    async updateCourse() {
        let { grade, sc_id } = this.ctx.request.body;
        let sql = `UPDATE stu_course SET grade=${grade} WHERE sc_id =${sc_id}`;
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '学生成绩修改成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 可选课程
     * @msg:  '未选课列表查询成功'
     * @param {*} stu_id
     * @return {*}
     */
    async checkCourse() {
        let { stu_id } = this.ctx.query;
        let sql = `SELECT * FROM teacher_course WHERE tc_id not in (SELECT tc_id FROM stu_course where stu_id = ${stu_id})`;
        const results = await this.app.mysql.query(sql);
        //console.log(sql)
        this.ctx.body = {
            code: '200',
            msg: '未选课列表查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 选择课程
     * @msg: 
     * @param {*}stu_id tc_id
     * @return {*}
     */
    async insertCourse() {
        let { stu_id, tc_id } = this.ctx.request.body;
        let sql = `INSERT into stu_course(stu_id,tc_id)  VALUES (${stu_id}, ${tc_id})`;
        const results = await this.app.mysql.query(sql);
        //console.log(sql);
        this.ctx.body = {
            code: '200',
            msg: '课程选择成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 查询学生已选课程
     * @msg: '学生已选课程查询成功'
     * @param {*} stu_id
     * @return {*}
     */
    async selStuView() {
        let { stu_id } = this.ctx.query;
        let sql = `SELECT * FROM stu_grade_view WHERE stu_id = ${stu_id}`;
        const results = await this.app.mysql.query(sql);
        this.ctx.body = {
            code: '200',
            msg: '学生已选课程查询成功',
            data: results,
            result: true
        }
    }
}

module.exports = CourseController
